<template>
  <Modal v-model:visible="showModal" title="师市人口汇聚情况" width="1100px">
    <div class="flex flex-col">
      <div class="flex flex-row items-center mb-1">
        <a-select
          v-model:value="searchParams.year"
          :options="utils.genSelectOptions('year')"
          @change="loadData"
        ></a-select>
      </div>
      <vxe-table
        :loading="true"
        v-bind="customTableStyle"
        :data="tableData"
        class="custom-vxe-table"
      >
        <vxe-column field="year" title="年份" width="80">
          <template #default="{ row }"> {{ row.year }}年 </template>
        </vxe-column>
        <vxe-column field="month" title="月份" width="80"></vxe-column>
        <vxe-column field="zcCount" title="实有人口(人)" :sortable="true">
          <template #default="{ row }">
            {{ row.zcCount || 0 }}
          </template>
        </vxe-column>
        <vxe-column field="hjCount" title="户籍人口(人)" :sortable="true">
          <template #default="{ row }">
            {{ row.hjCount || 0 }}
          </template>
        </vxe-column>
        <vxe-column field="flowInCount" title="流入人口(人)" :sortable="true">
          <template #default="{ row }">
            {{ row.flowInCount || 0 }}
          </template>
        </vxe-column>
        <vxe-column field="flowOutCount" title="流出人口(人)" :sortable="true">
          <template #default="{ row }">
            {{ row.flowOutCount || 0 }}
          </template>
        </vxe-column>
        <vxe-column field="flowInPercentage" title="流入人口占比" :sortable="true">
          <template #default="{ row }"> {{ row.flowInPercentage }}% </template>
        </vxe-column>
        <vxe-column field="flowOutPercentage" title="流出人口占比" :sortable="true">
          <template #default="{ row }"> {{ row.flowOutPercentage }}% </template>
        </vxe-column>
      </vxe-table>
    </div>
  </Modal>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import Modal from '@/components/modal.vue'
import { Apis } from '@/api'
import { message } from 'ant-design-vue'
import * as utils from '../../utils'

const isLoading = ref(false)

const showModal = ref(false)

const searchParams = ref({
  year: '',
  month: 12,
})
const tableData = ref<any[]>([])

// 统一管理表格样式
const customTableStyle = {
  ...utils.customTableStyle,
  footerMethod: ({ columns, data }) => {
    const footerData = [
      columns.map((column, columnIndex) => {
        if (columnIndex === 0) {
          return '合计'
        }
        if (['flowInCount', 'flowOutCount'].includes(column.field)) {
          return (utils.sumNum(data, column.field) || '') + '人'
        }

        return ''
      }),
    ]
    return footerData
  },
}

function loadData() {
  isLoading.value = true
  Apis.szbt.populationData
    .get_getChartData({
      params: {
        year: searchParams.value.year,
        month: 12,
        week: 0,
      },
    })
    .then((res) => {
      tableData.value = (res.months || [])
        .filter((item) => item.year == searchParams.value.year)
        .sort((a: any, b: any) => {
          // 再按月份倒序
          if (b.month !== a.month) {
            return b.month - a.month
          }
          return b.zcCount - a.zcCount
        })
        .map((item: any, index: number) => {
          item.index = index + 1
          const zcCount = item.zcCount || 0
          item.zcCount = zcCount
          item.flowInPercentage = zcCount
            ? (((item.flowInCount || 0.0) / zcCount) * 100).toFixed(2)
            : 0.0 // 计算流入占总人口的比例
          item.flowOutPercentage = zcCount
            ? (((item.flowOutCount || 0.0) / zcCount) * 100).toFixed(2)
            : 0.0 // 计算流出占总人口的比例
          return item
        })
    })
    .catch((err) => {
      message.error('获取数据失败')
    })
    .finally(() => {
      isLoading.value = false
    })
}

defineExpose({
  setData: (data: any) => {
    const { params } = data
    searchParams.value = params
    // tableData.value = data;
  },
  open: () => {
    showModal.value = true
    loadData()
  },
})
</script>

<style lang="scss" src="@/styles/custom-vxe-table.scss"></style>
